package com.rc.evcharger.service.discount;

import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.constant.level.UserLevelConstant;
import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.evcharger.mapper.discount.TenantDiscountTimeMapper;
import com.rc.evcharger.mapper.member.MemberInfoMapper;
import com.rc.evcharger.mapper.order.OrderDetailMapper;
import com.rc.evcharger.model.discount.TenantDiscountPolicy;
import com.rc.evcharger.model.discount.TenantDiscountTime;
import com.rc.evcharger.model.elec.ElecStationPrice;
import com.rc.evcharger.model.hlht.HlhtOrderInfo;
import com.rc.evcharger.model.member.MemberInfo;
import com.rc.evcharger.model.order.OrderDetail;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.service.elec.ElecStationPriceService;
import com.rc.evcharger.service.ugs.TenantUgsMemberLevelService;
import com.rc.evcharger.vo.cost.UserLevelPriceResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class TenantDiscountPolicyServiceImpl implements TenantDiscountPolicyService {
    Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Autowired
    private TenantDiscountMemberStationService tenantDiscountMemberStationService;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private TenantDiscountTimeMapper tenantDiscountTimeMapper;

    @Autowired
    private TenantUgsMemberLevelService tenantUgsMemberLevelService;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private ElecStationPriceService elecStationPriceService;

    @Override
    public TenantDiscountPolicyMapper getMapper() {
        return tenantDiscountPolicyMapper;
    }

    /**
     * 查询有效活动方案
     *
     * @param discountPolicyId 方案ID
     * @param date             有效时间
     * @param discountType
     * @return
     */
    @Override
    public TenantDiscountPolicy selectValidDPolicy(Long discountPolicyId, Date date, String discountType) {
        return tenantDiscountPolicyMapper.selectValidDPolicy(discountPolicyId, date, discountType);
    }


    @Override
    public TenantDiscountPolicy selectByMemberIdAndEsId(Long memberId, Long stationId, BigDecimal totalSeviceMoney) {

        TenantDiscountPolicy personTdp0 = this.selectPersonalByMemberIdAndEsId(memberId, stationId, totalSeviceMoney);

        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(memberId);
        MemberInfo enterMemberInfo = null;
        //本身是企业主会员
        if (SocketConstant.STR_CON1.equals(memberInfo.getMemberType())) {
            enterMemberInfo = memberInfo;
        } else {
            //获取充电主会员
            enterMemberInfo = memberInfoMapper.selectByPrimaryKey(memberInfo.getEnterpriseMemberId());
        }

        TenantDiscountPolicy enterpriseTdp1 = tenantDiscountPolicyMapper.selectByEnterMemberIdAndEsId(enterMemberInfo.getMemberId(), stationId);
        //无优惠
        if (personTdp0 == null && enterpriseTdp1 == null) {
            return null;
        }

        //返回企业折扣
        if (personTdp0 == null && enterpriseTdp1 != null) {
            if (enterpriseTdp1.getEnterpriseMemberId().longValue() != enterMemberInfo.getMemberId()) {
                return null;
            }
            return enterpriseTdp1;
        }

        //返回优惠方案
        if (personTdp0 != null && enterpriseTdp1 == null) {
            //针对全体用户
            if (personTdp0.getPolicyMemberType().equals(UserLevelConstant.POLICY_MEMBER_TYPE_TOTAL)) {
                return personTdp0;
            } else {
                //指定用户
                TenantDiscountTime tenantDiscountTime = tenantDiscountMemberStationService.selectByMemberIdAndEsId(memberId, stationId, personTdp0.getDiscountPolicyId());
                if (tenantDiscountTime == null) {
                    return null;
                } else {
                    return personTdp0;
                }
            }
        }


        int flag = 0;
        //针对全体用户
        if (personTdp0.getPolicyMemberType().equals(UserLevelConstant.POLICY_MEMBER_TYPE_TOTAL)) {
            flag = 1;
        } else {
            //指定用户
            TenantDiscountTime tenantDiscountTime = tenantDiscountMemberStationService.selectByMemberIdAndEsId(memberId, stationId, personTdp0.getDiscountPolicyId());
            if (tenantDiscountTime == null) {
                return enterpriseTdp1;
            } else {
                flag = 1;
            }
        }
        if (flag == 0) {
            return enterpriseTdp1;
        }


        //获取优惠单价
        String time = DateHelper.dataToString(new Date(), DateHelper.DATE_COMMON_FORMAT);
        TenantDiscountTime personDiscountTime0 = tenantDiscountTimeMapper.selectValidTime(personTdp0.getDiscountPolicyId(), time);
        TenantDiscountTime enterpriseDiscountTime1 = tenantDiscountTimeMapper.selectValidTime(enterpriseTdp1.getDiscountPolicyId(), time);
        if (personDiscountTime0 == null && enterpriseDiscountTime1 == null) {
            logger.error("149对应活动方案时段已过期:对应方案ID:{},ID:{}", personTdp0.getDiscountPolicyId(), enterpriseDiscountTime1);
            return null;
        }
        if (personDiscountTime0 != null && enterpriseDiscountTime1 == null) {
            return personTdp0;
        }
        if (personDiscountTime0 == null && enterpriseDiscountTime1 != null) {
            return enterpriseTdp1;
        }


        ElecStationPrice elecStationPrice = elecStationPriceService.selectValidElecStationPrice(stationId);

        //服务费折扣比较
        if (SocketConstant.STR_CON0.equals(personTdp0.getDiscountType())) {

            //企业服务费打折
            if (SocketConstant.STR_CON0.equals(enterpriseTdp1.getDiscountType())) {
                if (personDiscountTime0.getDiscount().compareTo(enterpriseDiscountTime1.getDiscount()) <= 0) {
                    return personTdp0;
                } else {
                    return enterpriseTdp1;
                }
            } else {
                //企业总折扣打折
                BigDecimal enterPrice = elecStationPrice.getServicePrice().add(elecStationPrice.getElecPrice()).multiply(enterpriseDiscountTime1.getDiscount());
                //优惠方案
                BigDecimal personPrice = elecStationPrice.getElecPrice().add(elecStationPrice.getServicePrice().multiply(personDiscountTime0.getDiscount()));

                //相等情况下，按优惠方案价>超级会员>企业折扣价>会员等级价>优惠券
                if (enterPrice.compareTo(personPrice) >= 0) {
                    return personTdp0;
                } else {
                    return enterpriseTdp1;
                }
            }
        }

        //活动价比较
        if (SocketConstant.STR_CON1.equals(personTdp0.getDiscountType())) {

            //活动价
            BigDecimal activityPrice = personDiscountTime0.getElecPrice().add(personDiscountTime0.getServicePrice());

            //企业服务费打折
            if (SocketConstant.STR_CON0.equals(enterpriseTdp1.getDiscountType())) {

                BigDecimal enterPrice = elecStationPrice.getElecPrice().add(elecStationPrice.getServicePrice().multiply(enterpriseDiscountTime1.getDiscount()));

                if (enterPrice.compareTo(activityPrice) > 0) {
                    return personTdp0;
                } else {
                    return enterpriseTdp1;
                }

            } else {
                //企业总折扣打折
                BigDecimal enterPrice = elecStationPrice.getServicePrice().add(elecStationPrice.getElecPrice()).multiply(enterpriseDiscountTime1.getDiscount());

                //相等情况下，按优惠方案价>超级会员>企业折扣价>会员等级价>优惠券
                if (enterPrice.compareTo(activityPrice) >= 0) {
                    return personTdp0;
                } else {
                    return enterpriseTdp1;
                }
            }
            //BigDecimal servicePrice = elecStationPrice.getServicePrice().multiply(enterpriseDiscountTime1.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);
            ////企业折扣价
            //BigDecimal totalPrice = elecStationPrice.getElecPrice().add(servicePrice);
            ////活动价
            //BigDecimal activityPrice = personDiscountTime0.getElecPrice().add(personDiscountTime0.getServicePrice());
            ////获取较小值
            //if (totalPrice.compareTo(activityPrice) > 0) {
            //    return personTdp0;
            //} else {
            //    return enterpriseTdp1;
            //}
        }
        logger.error("结算时产生无效优惠,对应会员:{}", memberId);
        return null;
    }

    /**
     * @param memberId
     * @param stationId
     * @param serviceAmount
     * @return 返回有效个人有效方案，不包括企业VIP
     */
    @Override
    public TenantDiscountPolicy selectPersonalByMemberIdAndEsId(Long memberId, Long stationId, BigDecimal serviceAmount) {
        //得到站点有效方案
        //TenantDiscountPolicy personTdp0 = tenantDiscountPolicyMapper.selectVsealidDPolicyByEsId(stationId, UserLevelConstant.POLICY_MEMBER_TYPE_PERSON);

        List<TenantDiscountPolicy> personTdp0 = tenantDiscountPolicyMapper.selectVsealidDPolicyByEsIdList(stationId, UserLevelConstant.POLICY_MEMBER_TYPE_PERSON);

        if (CollectionUtils.isEmpty(personTdp0)) {
            return null;
        }

        for (TenantDiscountPolicy tenantDiscountPolicy : personTdp0) {
            //无优惠
            if (personTdp0 == null) {
                continue;
            }

            //针对全体用户
            if (tenantDiscountPolicy.getPolicyMemberType().equals(UserLevelConstant.POLICY_MEMBER_TYPE_TOTAL)) {
                return tenantDiscountPolicy;
            } else {
                //指定用户
                TenantDiscountTime tenantDiscountTime = tenantDiscountMemberStationService.selectByMemberIdAndEsId(memberId, stationId, tenantDiscountPolicy.getDiscountPolicyId());
                if (tenantDiscountTime == null) {
                    continue;
                } else {
                    return tenantDiscountPolicy;
                }
            }
        }
        return null;
    }


    /**
     * @param memberId
     * @param stationId
     * @param memberType   0个人 1企业主帐户 2企业子帐户 3互联互通用户
     * @param personalType 0普通会员  1商户用户
     * @return 特定服务费、企业VIP、活动电价 三选一
     */
    @Override
    public UserLevelPriceResult selectBestByMemberIdAndEsId(OrderInfo orderInfo, String personalType) {
        //服务费为0不计入折扣
        if (orderInfo.getTotalMoney().compareTo(BigDecimal.ZERO) <= 0) {
            return UserLevelPriceResult.emptyResult();
        }
        TenantDiscountPolicy tenantDiscountPolicy = null;


        //普通用户 特定服务费、活动电价 二选一
        if (SocketConstant.STR_CON0.equals(personalType)) {
            tenantDiscountPolicy = this.selectPersonalByMemberIdAndEsId(orderInfo.getMemberId(), orderInfo.getElecStationId(), orderInfo.getTotalSeviceMoney());
        } else {
            //特定服务费、企业VIP、活动电价 三选一
            tenantDiscountPolicy = this.selectByMemberIdAndEsId(orderInfo.getMemberId(), orderInfo.getElecStationId(), orderInfo.getTotalSeviceMoney());
        }

        if (tenantDiscountPolicy == null) {
            return UserLevelPriceResult.emptyResult();
        }

        //获取优惠单价
        String time = DateHelper.dataToString(new Date(), DateHelper.DATE_COMMON_FORMAT);
        TenantDiscountTime tenantDiscountTime = tenantDiscountTimeMapper.selectValidTime(tenantDiscountPolicy.getDiscountPolicyId(), time);
        if (tenantDiscountTime == null) {
            logger.error("对应活动方案时段已过期:对应方案ID:{}", tenantDiscountPolicy.getDiscountPolicyId());
            return UserLevelPriceResult.emptyResult();
        }

        //todo 专享活动价优先级最高
        if (UserLevelConstant.APPOINT_TYPE_TOTAL.equals(tenantDiscountPolicy.getDiscountType())) {

            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(tenantDiscountTime.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.APPOINT_DISCOUNT_PRICE);
            userLevelPriceResult.setElecPrice(tenantDiscountTime.getElecPrice());
            userLevelPriceResult.setServicePrice(tenantDiscountTime.getServicePrice());
            userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());

            UserLevelPriceResult userLevelPriceResult0 = this.countAppointDiscountAmountByPolicy(tenantDiscountPolicy.getDiscountPolicyId(), orderInfo.getOrderId(), orderInfo.getTotalMoney());

            if (userLevelPriceResult0.getType() == UserLevelConstant.APPOINT_DISCOUNT_PRICE) {
                userLevelPriceResult.setAppoingServiceAmount(userLevelPriceResult0.getAppoingServiceAmount());
                userLevelPriceResult.setAppointElecAmount(userLevelPriceResult0.getAppointElecAmount());
                return userLevelPriceResult; //返回特定价
            }

        }


        //优惠用户类型 0 全部 1指定用户 2企业用户
        if (SocketConstant.STR_CON2.equals(tenantDiscountPolicy.getPolicyMemberType())) {
            //总折扣
            if (SocketConstant.STR_CON2.equals(tenantDiscountPolicy.getDiscountType())) {

                UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
                userLevelPriceResult.setTenantCode(tenantDiscountTime.getTenantCode());
                userLevelPriceResult.setType(UserLevelConstant.ALL_SERVICE_RELIEF);
                userLevelPriceResult.setDiscountPrice(tenantDiscountTime.getDiscount());
                userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());

                //计算优惠服务费金额
                BigDecimal payMoney = orderInfo.getTotalMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);
                userLevelPriceResult.setDiscountAmount(orderInfo.getTotalMoney().subtract(payMoney));
                return userLevelPriceResult;    //返回总服务费折扣

            } else {
                UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
                userLevelPriceResult.setTenantCode(tenantDiscountTime.getTenantCode());
                userLevelPriceResult.setType(UserLevelConstant.ENTERPRISE_VIP);
                userLevelPriceResult.setDiscountPrice(tenantDiscountTime.getDiscount());
                userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());

                //计算优惠服务费金额
                BigDecimal saleTotalServiceMoney = orderInfo.getTotalSeviceMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);
                userLevelPriceResult.setDiscountAmount(orderInfo.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
                return userLevelPriceResult;    //返回企业VIP
            }

            //优惠类型, 0服务费打折 1 特定服务费（活动价） 2总价打折 默认0
        } else if (UserLevelConstant.DISCOUNT_TYPE_ACTIVITY.equals(tenantDiscountPolicy.getDiscountType())) {

            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(tenantDiscountTime.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.ACTIVITY_PRICE);
            userLevelPriceResult.setElecPrice(tenantDiscountTime.getElecPrice());
            userLevelPriceResult.setServicePrice(tenantDiscountTime.getServicePrice());
            userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());

            BigDecimal bigDecimal = this.countActivityDiscountAmountByPolicy(tenantDiscountPolicy.getDiscountPolicyId(), orderInfo.getOrderId(), orderInfo.getTotalMoney());
            userLevelPriceResult.setDiscountAmount(bigDecimal);

            return userLevelPriceResult; //返回活动价
        } else {

            UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
            userLevelPriceResult.setTenantCode(tenantDiscountTime.getTenantCode());
            userLevelPriceResult.setType(UserLevelConstant.SPECIFIC_SERVICE_RELIEF);
            userLevelPriceResult.setDiscountPrice(tenantDiscountTime.getDiscount());
            userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());

            //计算优惠服务费金额
            BigDecimal saleTotalServiceMoney = orderInfo.getTotalSeviceMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);
            userLevelPriceResult.setDiscountAmount(orderInfo.getTotalSeviceMoney().subtract(saleTotalServiceMoney));
            return userLevelPriceResult;  //返回服务费折扣
        }
    }

    /**
     * @param orderInfo
     * @param personalType 0普通会员  1商户用户
     * @return
     */
    @Override
    public UserLevelPriceResult selectBestBusinessByMemberIdAndEsId(OrderInfo orderInfo, String personalType) {
        if (orderInfo.getPayAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return UserLevelPriceResult.emptyResult();
        }

        //特定服务费、企业VIP、活动电价 三选一 or 特定服务费、活动电价二选一
        UserLevelPriceResult userLevelPriceResult = this.selectBestByMemberIdAndEsId(orderInfo, personalType);
        //会员价、超级会员价
        UserLevelPriceResult userLevelPriceResultLV = tenantUgsMemberLevelService.selectBestByesIdAndMemberId(orderInfo);

        //todo 专享优惠优先级最高
        if (userLevelPriceResult.getType() == UserLevelConstant.APPOINT_DISCOUNT_PRICE) {
            return userLevelPriceResult;
        }

        if (userLevelPriceResult.getType() == UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResultLV.getType() == UserLevelConstant.INVALID_DISCOUNT) {
            return UserLevelPriceResult.emptyResult();
        }

        if (userLevelPriceResult.getType() != UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResultLV.getType() == UserLevelConstant.INVALID_DISCOUNT) {
            return userLevelPriceResult;
        }

        if (userLevelPriceResult.getType() == UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResultLV.getType() != UserLevelConstant.INVALID_DISCOUNT) {
            return userLevelPriceResultLV;
        }

        //相等情况下，按优惠方案价>超级会员>企业折扣价>会员等级价>优惠券
        if (userLevelPriceResult.getDiscountAmount().compareTo(userLevelPriceResultLV.getDiscountAmount()) > 0) {
            return userLevelPriceResult;
        } else if (userLevelPriceResult.getDiscountAmount().compareTo(userLevelPriceResultLV.getDiscountAmount()) < 0) {
            return userLevelPriceResultLV;
        } else if (userLevelPriceResult.getDiscountAmount().compareTo(userLevelPriceResultLV.getDiscountAmount()) == 0) {
            //相等情况下比较优先级
            if (userLevelPriceResultLV.getType() == UserLevelConstant.USER_LEVEL_PRICE) {
                return userLevelPriceResult;
            } else if (userLevelPriceResult.getType() == UserLevelConstant.ENTERPRISE_VIP && userLevelPriceResultLV.getType() == UserLevelConstant.USER_LEVEL_PRICE_SVIP) {
                return userLevelPriceResultLV;
            } else {
                return userLevelPriceResult;
            }
        }

        return UserLevelPriceResult.emptyResult();
    }

    /**
     * 查询该站点优惠方案是否包含全部会员
     *
     * @param elecStationId
     * @return
     */
    @Override
    public TenantDiscountPolicy selectTotalMember(Long elecStationId, Date date, String discountType) {
        return tenantDiscountPolicyMapper.selectTotalMember(elecStationId, date, discountType);
    }

    /**
     * 获取活动价方案的优惠金额
     *
     * @param discountPolicyId
     * @param orderId
     * @param totalMoney
     * @return
     */
    @Override
    public BigDecimal countActivityDiscountAmountByPolicy(Long discountPolicyId, Long orderId, BigDecimal totalMoney) {

        //得到分时段订单
        List<OrderDetail> orderDetails = orderDetailMapper.listByOrderId(orderId);

        //进行比较
        if (!CollectionUtils.isEmpty(orderDetails)) {
            //活动电价总金额
            BigDecimal activityChargeAmount = BigDecimal.ZERO;

            //遍历分时段计算折扣
            for (OrderDetail orderRecord : orderDetails) {

                //得到有效方案的时段,按分时段订单的开始时间计算
                String time = DateHelper.dataToString(orderRecord.getBeginTime(), DateHelper.DATE_COMMON_FORMAT);
                TenantDiscountTime tenantDiscountTime = tenantDiscountTimeMapper.selectValidTime(discountPolicyId, time);
                if (tenantDiscountTime == null) {
                    continue;
                }
                //防止异常分时段
                if (orderRecord.getElecAmount().compareTo(BigDecimal.ZERO) < 0) {
                    continue;
                }

                //防止单价一致，却产生误差
                if (tenantDiscountTime.getElecPrice().compareTo(orderRecord.getElecPrice()) == 0
                        && tenantDiscountTime.getServicePrice().compareTo(orderRecord.getServicePrice()) == 0) {

                    activityChargeAmount = activityChargeAmount.add(orderRecord.getChargeAmount());
                } else {
                    BigDecimal serviceAmount = tenantDiscountTime.getServicePrice().multiply(orderRecord.getPower());
                    BigDecimal baseAmount = tenantDiscountTime.getElecPrice().multiply(orderRecord.getPower());
                    activityChargeAmount = activityChargeAmount.add(serviceAmount).add(baseAmount).setScale(2, BigDecimal.ROUND_HALF_UP);

                }
            }

            //活动价的优惠金额
            BigDecimal activityDiscountAmount = totalMoney.subtract(activityChargeAmount);
            if (activityChargeAmount.compareTo(BigDecimal.ZERO) == 0) {
                return BigDecimal.ZERO;
            }

            return activityDiscountAmount;
        }
        logger.error("对应活动方案时段已过期:对应订单ID:{}", orderId);
        return BigDecimal.ZERO;
    }


    @Override
    public UserLevelPriceResult countAppointDiscountAmountByPolicy(Long discountPolicyId, Long orderId, BigDecimal totalMoney) {

        //得到分时段订单
        List<OrderDetail> orderDetails = orderDetailMapper.listByOrderId(orderId);

        UserLevelPriceResult userLevelPriceResult = UserLevelPriceResult.emptyResult();

        //进行比较
        if (!CollectionUtils.isEmpty(orderDetails)) {
            //特定价格总金额
            BigDecimal appointChargeAmount = BigDecimal.ZERO;
            //特定价格服务费金额
            BigDecimal appointElecAmount = BigDecimal.ZERO;
            //特定价格电费金额
            BigDecimal appointServiceAmount = BigDecimal.ZERO;

            //遍历分时段计算折扣
            for (OrderDetail orderRecord : orderDetails) {

                //得到有效方案的时段,按分时段订单的开始时间计算
                String time = DateHelper.dataToString(orderRecord.getBeginTime(), DateHelper.DATE_COMMON_FORMAT);
                TenantDiscountTime tenantDiscountTime = tenantDiscountTimeMapper.selectValidTime(discountPolicyId, time);
                if (tenantDiscountTime == null) {
                    continue;
                }
                //防止异常分时段
                if (orderRecord.getElecAmount().compareTo(BigDecimal.ZERO) < 0) {
                    continue;
                }

                //防止单价一致，却产生误差
                if (tenantDiscountTime.getElecPrice().compareTo(orderRecord.getElecPrice()) == 0
                        && tenantDiscountTime.getServicePrice().compareTo(orderRecord.getServicePrice()) == 0) {

                    appointElecAmount = appointElecAmount.add(orderRecord.getElecAmount());
                    appointServiceAmount = appointServiceAmount.add(orderRecord.getServiceAmount());
                    appointChargeAmount = appointChargeAmount.add(orderRecord.getChargeAmount());
                } else {
                    BigDecimal serviceAmount = tenantDiscountTime.getServicePrice().multiply(orderRecord.getPower()).setScale(2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal elecAmount = tenantDiscountTime.getElecPrice().multiply(orderRecord.getPower()).setScale(2, BigDecimal.ROUND_HALF_UP);

                    appointElecAmount = appointElecAmount.add(elecAmount);
                    appointServiceAmount = appointServiceAmount.add(serviceAmount);
                    appointChargeAmount = appointChargeAmount.add(serviceAmount).add(elecAmount).setScale(2, BigDecimal.ROUND_HALF_UP);

                    //分时段赋值
                    orderRecord.setServiceAmount(serviceAmount);
                    orderRecord.setServicePrice(tenantDiscountTime.getServicePrice());
                    orderRecord.setElecAmount(elecAmount);
                    orderRecord.setElecPrice(tenantDiscountTime.getElecPrice());
                    //分时段总金额
                    orderRecord.setChargeAmount(orderRecord.getElecAmount().add(orderRecord.getServiceAmount()));
                    orderRecord.setRemark("特定价");
                    //更新特定价格分时段订单
                    orderDetailMapper.updateByPrimaryKeySelective(orderRecord);

                }
            }

            //特定价格的优惠金额
            BigDecimal appointDiscountAmount = totalMoney.subtract(appointChargeAmount);
            if (appointDiscountAmount.compareTo(BigDecimal.ZERO) == 0) {
                return UserLevelPriceResult.emptyResult();
            }

            //赋值返回
            userLevelPriceResult.setType(UserLevelConstant.APPOINT_DISCOUNT_PRICE);
            userLevelPriceResult.setAppointElecAmount(appointElecAmount);
            userLevelPriceResult.setAppoingServiceAmount(appointServiceAmount);

            return userLevelPriceResult;
        }
        logger.error("对应活动方案时段已过期:对应订单ID:{}", orderId);
        return UserLevelPriceResult.emptyResult();
    }


    @Override
    public UserLevelPriceResult compareTicketDisAmount(UserLevelPriceResult userLevelPriceResult0, UserLevelPriceResult userLevelPriceResult1) {
        //专享优惠价优先级最高
        if (userLevelPriceResult1.getType() == UserLevelConstant.APPOINT_DISCOUNT_PRICE) {
            return userLevelPriceResult1;
        }

        if (userLevelPriceResult0.getType() == UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResult1.getType() == UserLevelConstant.INVALID_DISCOUNT) {
            return UserLevelPriceResult.emptyResult();
        }

        if (userLevelPriceResult0.getType() != UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResult1.getType() == UserLevelConstant.INVALID_DISCOUNT) {
            return userLevelPriceResult0;
        }

        if (userLevelPriceResult0.getType() == UserLevelConstant.INVALID_DISCOUNT && userLevelPriceResult1.getType() != UserLevelConstant.INVALID_DISCOUNT) {
            return userLevelPriceResult1;
        }

        if (userLevelPriceResult0.getDiscountAmount().compareTo(userLevelPriceResult1.getDiscountAmount()) > 0) {
            return userLevelPriceResult0;
        } else if (userLevelPriceResult0.getDiscountAmount().compareTo(userLevelPriceResult1.getDiscountAmount()) == 0) {
            return userLevelPriceResult1;
        } else {
            return userLevelPriceResult1;
        }
    }

    //查询互联互通折扣
    @Override
    public UserLevelPriceResult selectHlhtValidDPolicyByEsId(OrderInfo orderInfo) {
        if (orderInfo.getPayAmount().compareTo(BigDecimal.ZERO) == 0) {
            return UserLevelPriceResult.emptyResult();
        }
        //查询互联互通折扣
        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyMapper.selectHlhtValidDPolicyByEsId(orderInfo.getElecStationId(), orderInfo.getMemberId());
        if (tenantDiscountPolicy == null) {
            return UserLevelPriceResult.emptyResult();
        }

        String time = DateHelper.dataToString(new Date(), DateHelper.DATE_COMMON_FORMAT);
        TenantDiscountTime tenantDiscountTime = tenantDiscountTimeMapper.selectValidTime(tenantDiscountPolicy.getDiscountPolicyId(), time);
        if (tenantDiscountTime == null) {
            return UserLevelPriceResult.emptyResult();
        }

        BigDecimal bigDecimal = orderInfo.getTotalSeviceMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);

        UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
        userLevelPriceResult.setDiscountPrice(tenantDiscountTime.getDiscount());
        userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());
        userLevelPriceResult.setType(UserLevelConstant.ENTERPRISE_VIP);
        userLevelPriceResult.setDiscountAmount(orderInfo.getTotalSeviceMoney().subtract(bigDecimal));
        return userLevelPriceResult;
    }

    @Override
    public UserLevelPriceResult selectAppointByEsId(HlhtOrderInfo hlhtOrderInfo) {
        return null;
    }

    @Override
    public UserLevelPriceResult selectHlhtValidDPolicyByEsId(HlhtOrderInfo hlhtOrderInfo) {
        if (hlhtOrderInfo.getPayAmount().compareTo(BigDecimal.ZERO) == 0) {
            return UserLevelPriceResult.emptyResult();
        }

        //根据瑞晨互联用户，查询互联互通折扣（港博设置的折扣）
        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyMapper.selectHlhtValidDPolicyByEsId(hlhtOrderInfo.getElecStationId(), hlhtOrderInfo.getMemberId());
        if (tenantDiscountPolicy == null) {
            return UserLevelPriceResult.emptyResult();
        }

        String time = DateHelper.dataToString(new Date(), DateHelper.DATE_COMMON_FORMAT);
        TenantDiscountTime tenantDiscountTime = tenantDiscountTimeMapper.selectValidTime(tenantDiscountPolicy.getDiscountPolicyId(), time);
        if (tenantDiscountTime == null) {
            return UserLevelPriceResult.emptyResult();
        }

        BigDecimal bigDecimal = hlhtOrderInfo.getTotalSeviceMoney().multiply(tenantDiscountTime.getDiscount()).setScale(2, BigDecimal.ROUND_HALF_UP);

        UserLevelPriceResult userLevelPriceResult = new UserLevelPriceResult();
        userLevelPriceResult.setDiscountPrice(tenantDiscountTime.getDiscount());
        userLevelPriceResult.setId(tenantDiscountPolicy.getDiscountPolicyId());
        userLevelPriceResult.setType(UserLevelConstant.ENTERPRISE_VIP);
        userLevelPriceResult.setDiscountAmount(hlhtOrderInfo.getTotalSeviceMoney().subtract(bigDecimal));
        return userLevelPriceResult;
    }
}
